package Day1

import (
	"fmt"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

type UserInfo struct {
	Name string
	Age  int
}

func Hello(ctx *gin.Context) {
	name := "xiaoming"
	ctx.HTML(200, "/index/index.html", name)
	// ctx.String(200, "hello world")
}

func User(ctx *gin.Context) {
	ctx.HTML(200, "/user/user.html", nil)
	// ctx.String(200, "hello world")
}

func SetUseInfo(ctx *gin.Context) {
	UerInfo := UserInfo{Name: "xiaoming", Age: 11}
	ctx.HTML(200, "/chapter1/day1.html", UerInfo)
	// ctx.String(200, "hello world")
}

func Arr(ctx *gin.Context) {
	arr := [3]int{1, 2, 3}
	ctx.HTML(200, "/chapter1/arr.html", arr)
	// ctx.String(200, "hello world")
}

func StructArr(ctx *gin.Context) {
	struct_Arr := [3]UserInfo{{"xiaoming", 11}, {"xiaofang", 15}}
	ctx.HTML(200, "/chapter1/structarr.html", struct_Arr)
	// ctx.String(200, "hello world")
}

func MapTest(ctx *gin.Context) {
	map_test := map[string]string{"name": "xiaofang", "age": "12"}
	ctx.HTML(200, "/chapter1/map.html", map_test)
	// ctx.String(200, "hello world")
}

func MapStruct(ctx *gin.Context) {
	map_struct := map[string]UserInfo{"studentA": {"xiaoming", 11}, "studentB": {"xiaofang", 15}}
	ctx.HTML(200, "/chapter1/mapstruct.html", map_struct)
	// ctx.String(200, "hello world")
}

func SliceTest(ctx *gin.Context) {
	slicetest := []int{1, 2, 3, 4, 5}
	ctx.HTML(200, "/chapter1/slice.html", slicetest)
	// ctx.String(200, "hello world")
}

func ParasTest(ctx *gin.Context) {
	IdValue := ctx.Param("Id")
	ctx.String(200, "hello Id:%s", IdValue)
	// ctx.String(200, "hello world")
}

func QueryTest(ctx *gin.Context) {
	queryValue := ctx.Query("age")
	ctx.String(200, "hello Id:%s", queryValue)
	// ctx.String(200, "hello world")
}

//querydefault
func QueryDefault(ctx *gin.Context) {
	queryValue := ctx.DefaultQuery("name", "xiaoming")
	ctx.String(200, "hello Id:%s", queryValue)
	// ctx.String(200, "hello world")
}

//queryarr
func QueryArr(ctx *gin.Context) {
	queryValue := ctx.QueryArray("age")
	ctx.String(200, "hello Id:%v", queryValue)
	// ctx.String(200, "hello world")
}

//querymap
func QueryMap(ctx *gin.Context) {
	queryValue := ctx.QueryMap("user")
	ctx.String(200, "hello Id:%v", queryValue)
	// ctx.String(200, "hello world")
}

func Topost(ctx *gin.Context) {
	ctx.HTML(200, "/chapter1/topost.html", nil)
	// ctx.String(200, "hello world")
}

func Dopost(ctx *gin.Context) {
	name := ctx.PostForm("name")
	fmt.Println(name)
	passwd := ctx.PostForm("passwd")
	fmt.Println(passwd)
	ctx.String(200, "post success")
	// ctx.String(200, "hello world")
}

func Topost2(ctx *gin.Context) {
	ctx.HTML(200, "/chapter1/postarr.html", nil)
	// ctx.String(200, "hello world")
}

func PostMap(ctx *gin.Context) {
	name := ctx.PostForm("name")
	fmt.Println(name)
	passwd := ctx.PostForm("passwd")
	fmt.Println(passwd)
	age := ctx.DefaultPostForm("age", "11")
	ctx.String(200, "post success name:%v passwd:%v age:%v", name, passwd, age)

	mapval := ctx.PostFormArray("type")
	fmt.Print(mapval)

	mapvar := ctx.PostFormMap("user")
	fmt.Println(mapvar)
	// ctx.String(200, "hello world")
}

func Topost3(ctx *gin.Context) {
	ctx.HTML(200, "/chapter1/ajax.html", nil)
	// ctx.String(200, "hello world")
}

func PostAjax(ctx *gin.Context) {
	name := ctx.PostForm("name")
	fmt.Println(name)
	passwd := ctx.PostForm("passwd")
	fmt.Println(passwd)

	map_data := map[string]interface{}{
		"code": 200,
		"msg":  "成功",
	}

	ctx.JSON(200, map_data)
	// ctx.String(200, "hello world")
}

func Topost4(ctx *gin.Context) {
	ctx.HTML(200, "/chapter1/bind.html", nil)
	// ctx.String(200, "hello world")
}

type UserInfoNew struct {
	Name   string `form:"name"`
	Passwd string `form:"passwd"`
}

func PostBind(ctx *gin.Context) {
	var userInfoA UserInfoNew
	err := ctx.ShouldBind(&userInfoA)
	fmt.Print(err)
	fmt.Print(userInfoA)
	ctx.String(200, "UserInfo:%s", userInfoA.Name)
	// ctx.String(200, "hello world")
}

func ToSingeFile(ctx *gin.Context) {
	ctx.HTML(200, "/chapter1/single.html", nil)
	// ctx.String(200, "hello world")
}

func GetSinFile(ctx *gin.Context) {
	file, err := ctx.FormFile("file")
	if err != nil {
		fmt.Print(err)
	}

	time_int := time.Now().Unix()
	time_str := strconv.FormatInt(time_int, 10)
	dst := "svefile/" + time_str + file.Filename
	ctx.SaveUploadedFile(file, dst)

	ctx.String(200, "上传文件成功")
}

func ToFiles(ctx *gin.Context) {
	ctx.HTML(200, "/chapter1/files.html", nil)
	// ctx.String(200, "hello world")
}

func GetFiles(ctx *gin.Context) {
	form, _ := ctx.MultipartForm()
	files := form.File["file"]

	for _, file := range files {
		time_int := time.Now().Unix()
		time_str := strconv.FormatInt(time_int, 10)
		dst := "svefile/" + time_str + file.Filename
		ctx.SaveUploadedFile(file, dst)
	}

	ctx.String(200, "上传文件成功")
}

func OneAjax(ctx *gin.Context) {
	ctx.HTML(200, "/chapter1/oneajax.html", nil)
	// ctx.String(200, "hello world")
}

func GetOneAjax(ctx *gin.Context) {
	file, _ := ctx.FormFile("file")
	fmt.Println(file.Filename)
	time_int := time.Now().Unix()
	time_str := strconv.FormatInt(time_int, 10)
	dst := "svefile/" + time_str + file.Filename
	ctx.SaveUploadedFile(file, dst)
	// ctx.String(200, "上传文件成功")
}

func ToAjaxFiles(ctx *gin.Context) {
	ctx.HTML(200, "/chapter1/ajaxfiles.html", nil)
	// ctx.String(200, "hello world")
}

func GetAjaxFiles(ctx *gin.Context) {
	form, _ := ctx.MultipartForm()
	files := form.File["file"]

	for _, file := range files {
		time_int := time.Now().Unix()
		time_str := strconv.FormatInt(time_int, 10)
		dst := "svefile/" + time_str + file.Filename
		ctx.SaveUploadedFile(file, dst)
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"name": "ok",
	})
	// ctx.String(200, "上传文件成功")
}

func OutPutJson(ctx *gin.Context) {
	ctx.JSON(200, gin.H{
		"code": 200,
		"tag":  "br",
		"msg":  "提交成功",
		"name": "ok",
		"html": "<b>hello,world</b>",
	})
	// ctx.String(200, "上传文件成功")
}

func Proto(ctx *gin.Context) {

	// ctx.String(200, "上传文件成功")
}
